home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1994 November / macformat-018.iso / Utility Spectacular / Developer / SAT / SAT Invaders sample ƒ / sPlayer.p < prev    next >
Encoding:
Text File  |  1994-07-26  |  2.8 KB  |  104 lines  |  [TEXT/PJMM]

  1. {===============================================}
  2. {================= Player sprite unit ================}
  3. {===============================================}
  4.  
  5. { Example file for Ingemars Sprite Animation Toolkit. }
  6. { © Ingemar Ragnemalm 1992 }
  7. { See doc files for legal terms for using this code. }
  8.  
  9. { This file is the first of several sprite units, units that holds the full}
  10. {description of the objects to be animated. }
  11.  
  12. unit sPlayer;
  13.  
  14. { Sprite unit. A sprite unit should include the following routines:}
  15. { Init-procedure, that initializes private bitmaps}
  16. { Setup-procedure, that sets variables other than the standard ones set by MakeSprite }
  17. { Handle-procedure, to be called once per iteration until the sprite dies }
  18. { Hittask-procedure (optional), for advanced collission handling. }
  19.  
  20. { This is the sprite unit for the player object, in this case a cannon. }
  21.  
  22. interface
  23.  
  24.     uses
  25.         GameGlobals, SAT, SoundConst, sShot;
  26.  
  27.     var
  28.         stillRunning: boolean;
  29.  
  30.     procedure InitPlayer;
  31.     procedure SetupPlayer (player: SpritePtr);
  32.     procedure HandlePlayer (me: SpritePtr);
  33.  
  34. implementation
  35.  
  36.     const
  37.         playerspeed = 16; {How fast may the player object move per frame, max?}
  38.  
  39.     var
  40.         playerFace: FacePtr;
  41.  
  42.     procedure InitPlayer;
  43.     begin
  44.         playerFace := GetFace(134); {Preload the player face (from cicn resource)}
  45.     end;
  46.  
  47.     procedure SetupPlayer (player: SpritePtr);
  48.     begin
  49.         player^.face := playerFace;
  50.         SetRect(player^.hotRect, 1, 7, 31, 32);
  51.         player^.task := @HandlePlayer;
  52.     end;
  53.  
  54.     procedure HandlePlayer (me: SpritePtr);
  55.         var
  56.             pt: point;
  57.             shot: SpritePtr;
  58.     begin
  59. {We detect collisions with change in "kind". We could also have chosen to do this}
  60. {with a "hitTask", a callback proceure.}
  61.         if me^.kind <> 2 then
  62.             begin
  63.                 SATSoundPlay(kraschH, 10, true);        {Play a sound}
  64.                 stillrunning := false;                        {Tell MoveIt that the game is over, to quit the game loop.}
  65. { Real games make an explosion before quitting!}
  66.             end;
  67.  
  68. {Where is the mouse pointer?}
  69.         SATSetPortScreen;
  70.         GetMouse(pt);
  71.  
  72.         me^.speed.h := pt.h - me^.position.h; {How far from the previous position?}
  73.  
  74. {Change the position to the new position, but not by more than platerspeed!}
  75.         if me^.speed.h < -playerspeed then
  76.             me^.position.h := me^.position.h - playerspeed
  77.         else if me^.speed.h > playerspeed then
  78.             me^.position.h := me^.position.h + playerspeed
  79.         else
  80.             me^.position.h := me^.position.h + me^.speed.h;
  81.  
  82. {Make sure we don't go out of sight.}
  83.         if me^.position.h > gSAT.offSizeH - 32 then
  84.             begin
  85.                 me^.position.h := gSAT.offSizeH - 32;
  86.             end;
  87.         if me^.position.h < 0 then
  88.             begin
  89.                 me^.position.h := 0;
  90.             end;
  91.  
  92. { Create shots }
  93.         if me^.mode > 0 then
  94.             me^.mode := pred(me^.mode);
  95.         if me^.mode = 0 then {We may only shoot every 10 frames!}
  96.             if Button then
  97.                 begin
  98.                     shot := NewSprite(1, me^.position.h + 12, me^.position.v, @SetupShot);
  99.                     me^.mode := 10;
  100.                     SATSoundPlay(toffH, 1, false);
  101.                 end;
  102.     end;
  103.  
  104. end.